package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加用户端购物车
     *
     * @param dto
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
        //查数据库看是不是第一次添加
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        log.info("从线程中查到的当前用户id为:{}", userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByShoppingCart(shoppingCart);
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            ShoppingCart shoppingCart1 = shoppingCarts.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            //不是第一次添加就修改数据库内购物车数量
            shoppingCartMapper.update(shoppingCart1);
        } else {
            //是第一次添加就新增数据到数据库
            //先判断是添加菜品还是套餐
            log.info("查询操作后的shoppingCart的值为:{}", shoppingCart);
            if (dto.getDishId() != null) {
                List<Long> list = new ArrayList<>();
                list.add(dto.getDishId());
                List<Dish> dishList = dishMapper.seleteById(list);
                shoppingCart.setName(dishList.get(0).getName());
                shoppingCart.setAmount(dishList.get(0).getPrice());
                shoppingCart.setImage(dishList.get(0).getImage());
            } else {
                Setmeal setmeal = setmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
            log.info("插入操作后的shoppingCart的值为:{}", shoppingCart);
        }
    }

    /**
     * 查找当前用户全部购物车数量
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByShoppingCart(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 删除用户单一购物车数据
     *
     * @param shoppingCartDTO
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        //先查找数据库用户购物车中此商品的数量
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userid = BaseContext.getCurrentId();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userid);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByShoppingCart(shoppingCart);
       //如果要删除的商品数量大于一 则商品数量减一
        if (shoppingCartList.get(0).getNumber() > 1) {
            ShoppingCart shoppingCart1 = shoppingCartList.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartMapper.update(shoppingCart1);
        }
        else{
            shoppingCartMapper.delete(shoppingCart);
        }
    }

    /**
     * 删除用户购物车的所有商品
     */
    @Override
    public void deleteAll() {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);
        shoppingCartMapper.delete(shoppingCart);
    }
}
